<?php
if(!$_COOKIE["q_session"]){
	setcookie("q_session", md5(date("Y-m-d-H")));
}
if(!$_COOKIE["f_session"]){
	setcookie("f_session", md5(date("Y-m")));
}
session_start();
include_once( 'config.php' );
include_once( 'saetv2.ex.class.php' );

$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );

$uidGet = $c->get_uid();
$uid = $uidGet['uid'];

include_once("functions.php");

if(trim($_GET['name'])){
	$uidUserInfo = $c->show_user_by_name(trim($_GET['name']));
	$isUid = false;
	if($uidUserInfo["error_code"]){
		$uidUserInfo = $c->show_user_by_id(trim($_GET['name']));
		$isUid = true;
	}
}

if($_GET["post"]){
	$thistimeline = $c->show_status($_GET['post']);
	if($thistimeline["error_code"] == "20101"){
		$thistimeline = getCache($_GET['post']);
		$cachedPost = true;
	}
}

if(!$uid && $uidGet["error_code"]){
	header("Location: /login?error=".$uidGet["error_code"]);
	/*
	if($_COOKIE['token']){
		header("Location: /login?error=".$uidGet["error_code"]);
	}
	else{
		header("Location: /");
	}
	*/
}
else if(!$uid){
	header("Location: /login");
}
else if(!isset($_GET["cache"]) && ($_GET["name"] && !$uidUserInfo["id"] || $_GET["post"] && !$thistimeline["user"]["id"] || $_GET["friendships"] && $_GET["friendships"] != "followers" && $_GET["friendships"] != "friends")){
	header("Location: /");
}
?>